//
//  NSFileManager+ADUtil.m
//  ADKit
//
//  Created by duzexu on 2017/11/30.
//  Copyright © 2017年 duzexu. All rights reserved.
//

#import "NSFileManager+ADUtil.h"

@implementation NSFileManager (ADUtil)

+ (BOOL)ad_isPathExist:(NSString *)path {
    return [[NSFileManager defaultManager] fileExistsAtPath:path];
}

+ (BOOL)ad_isFileExist:(NSString *)path {
    BOOL isDirectory;
    return [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory] && !isDirectory;
}

+ (BOOL)ad_isDirectoryExist:(NSString *)path {
    BOOL isDirectory;
    return [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory;
}

+ (BOOL)ad_removeFile:(NSString *)path {
    return [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

+ (BOOL)ad_createDirectoryAtPath:(NSString *)path {
    return [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}

+ (NSString *)ad_documentFilePath:(NSString *)path {
    NSArray *paths = [path componentsSeparatedByString:@"/"];
    if (paths.count == 0) {
        return [self ad_documentPath];
    }else if (paths.count == 1) {
        return [[self ad_documentPath] stringByAppendingPathComponent:paths[0]];
    }else{
        NSMutableArray *dirs = paths.mutableCopy;
        [dirs removeLastObject];
        NSString *dir = [[self ad_documentPath] stringByAppendingPathComponent:[dirs componentsJoinedByString:@"/"]];
        if (![self ad_isDirectoryExist:dir]) {
            [self ad_createDirectoryAtPath:dir];
        }
        return [[self ad_documentPath] stringByAppendingPathComponent:[paths componentsJoinedByString:@"/"]];
    }
}

+ (NSString *)ad_libraryFilePath:(NSString *)path {
    NSArray *paths = [path componentsSeparatedByString:@"/"];
    if (paths.count == 0) {
        return [self ad_libraryPath];
    }else if (paths.count == 1) {
        return [[self ad_libraryPath] stringByAppendingPathComponent:paths[0]];
    }else{
        NSMutableArray *dirs = paths.mutableCopy;
        [dirs removeLastObject];
        NSString *dir = [[self ad_libraryPath] stringByAppendingPathComponent:[dirs componentsJoinedByString:@"/"]];
        if (![self ad_isDirectoryExist:dir]) {
            [self ad_createDirectoryAtPath:dir];
        }
        return [[self ad_libraryPath] stringByAppendingPathComponent:[paths componentsJoinedByString:@"/"]];
    }
}

+ (NSString *)ad_cachesFilePath:(NSString *)path {
    NSArray *paths = [path componentsSeparatedByString:@"/"];
    if (paths.count == 0) {
        return [self ad_cachesPath];
    }else if (paths.count == 1) {
        return [[self ad_cachesPath] stringByAppendingPathComponent:paths[0]];
    }else{
        NSMutableArray *dirs = paths.mutableCopy;
        [dirs removeLastObject];
        NSString *dir = [[self ad_cachesPath] stringByAppendingPathComponent:[dirs componentsJoinedByString:@"/"]];
        if (![self ad_isDirectoryExist:dir]) {
            [self ad_createDirectoryAtPath:dir];
        }
        return [[self ad_cachesPath] stringByAppendingPathComponent:[paths componentsJoinedByString:@"/"]];
    }
}

+ (NSUInteger)ad_fileCountInPath:(NSString *)path {
    NSUInteger count = 0;
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
    for (__unused NSString *fileName in fileEnumerator) {
        count += 1;
    }
    return count;
}

+ (unsigned long long)ad_folderSizeAtPath:(NSString *)path {
    __block unsigned long long totalFileSize = 0;
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
    for (NSString *fileName in fileEnumerator) {
        NSString *filePath = [path stringByAppendingPathComponent:fileName];
        NSDictionary *fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        totalFileSize += fileAttrs.fileSize;
    }
    return totalFileSize;
}

- (BOOL)ad_skipBackupAtPath:(NSString *)path {
    NSURL *url = [NSURL fileURLWithPath:path];
    
    if (!url || ![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        return NO;
    }
    
    NSError *error = nil;
    BOOL success = [url setResourceValue: [NSNumber numberWithBool:YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success) {
        NSLog(@"Error excluding %@ from backup %@", [url lastPathComponent], error);
    }
    return success;
}

#pragma mark User directory methods

+ (NSString *)ad_documentPath {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}

+ (NSString *)ad_libraryPath {
    return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}

+ (NSString *)ad_bundlePath {
    return [[NSBundle mainBundle] resourcePath];
}

+ (NSString *)ad_cachesPath {
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}

@end
